﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace PreProcessingLanguage
{
    public partial class MainForm : DevComponents.DotNetBar.Metro.MetroAppForm
    {
        private Ngram ng = new Ngram();
        private ProcessPragraph pp;
        public MainForm()
        {
            InitializeComponent();
            
            Shown += new EventHandler(Form1_Shown);

            // To report progress from the background worker we need to set this property
            backgroundWorker1.WorkerReportsProgress = true;
            // This event will be raised on the worker thread when the worker starts
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            // This event will be raised when we call ReportProgress
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

            
        }
        void Form1_Shown(object sender, EventArgs e)
        {
            // Start the background worker
            backgroundWorker1.RunWorkerAsync();
        }
        // On worker thread so do our thing!
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker1.ReportProgress(0);
            lbStatus.Text="Đang nạp dữ liệu...";
            if (ng.readData("1word_P.txt", "2word_P.txt", "3word_P.txt"))
            {
                lbStatus.Text = "Sẵn Sàng";
            }
            else
            {
                lbStatus.Text = "Có lỗi trong quá trình đọc dữ liệu";
            }
            try
            {
                ng.AllowDelValue = cbAllowDelValue.Checked;
                ng.ThresholdDel = double.Parse(txtThresholdDel.Text);
                ng.AllowDelCount = cbAllowDelCount.Checked;
                ng.CountDel = int.Parse(txtCountDel.Text);
            }
            catch (Exception)
            {
                lbStatus.Text = "Chưa config đầy đủ";
            }
            pp = new ProcessPragraph(ng);
            
            backgroundWorker1.ReportProgress(100);
            lbStatus.Text = "Hoàn thành, Sẵn sàng.";
        }
        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
        }

        private void editRTB(object sender, EventArgs e)
        {
            rtxtOutput.SelectedText = sender.GetType().Name;
        }

        private void buttonItem6_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Bạn có muốn thoát không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                Application.Exit();
        }

        private void rtxtInput_TextChanged(object sender, EventArgs e)
        {
            lbStatus.Text = "Textbox editing...";
            if (cbTyping.Checked && rtxtInput.Text != "" && rtxtInput.Text[rtxtInput.Text.Length - 1] != ' ' && !Regex.IsMatch(rtxtInput.Text[rtxtInput.Text.Length - 1]+"", "[.,;“”:()!?'`\\\"/] ?"))
            {
                rtxtOutput.Text = pp.process(rtxtInput.Text);
            }
        }

        private void bSaveClipboard_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(rtxtOutput.Text);
            lbStatus.Text = "Đã lưu vào clipboard";
        }

        private void bXoa_Click(object sender, EventArgs e)
        {
            rtxtInput.Text = "";
            rtxtOutput.Text = "";
        }

        private void rtxtOutput_MouseClick(object sender, MouseEventArgs e)
        {
            Console.WriteLine("OK");
            rtxtOutput.Select(rtxtOutput.SelectionStart, rtxtOutput.SelectionStart);
            string t = rtxtOutput.SelectedText;
            Console.WriteLine(rtxtOutput.SelectionStart + " " + rtxtOutput.SelectionStart+" "+t);
            Dictionary<string, double> words = null;
            if (ng.P1.ContainsKey(PreProcessing.ConvertToUnSign(t)))
            {
                words = ng.P1[PreProcessing.ConvertToUnSign(t)];
            }
            else
            {
                words = new Dictionary<string, double>();
                words.Add(PreProcessing.ConvertToUnSign(t), 1.0);
            }
            if (e.Button == MouseButtons.Right)
            {
                ContextMenu m = new ContextMenu();
                foreach (KeyValuePair<string, double> pair in words)
                {
                    m.MenuItems.Add(new MenuItem(pair.Key,new EventHandler(editRTB)));
                }

                m.Show(this, new Point(e.X, e.Y));
            }
        }

        private void bSave_Click_1(object sender, EventArgs e)
        {
            try
            {
                ng.AllowDelValue = cbAllowDelValue.Checked;
                ng.ThresholdDel = double.Parse(txtThresholdDel.Text);
                ng.AllowDelCount = cbAllowDelCount.Checked;
                ng.CountDel = int.Parse(txtCountDel.Text);
            }
            catch (Exception)
            {
                lbStatus.Text = "Chưa config đầy đủ";
            }
        }
    }
}